﻿@model PhoneBilling.Models.Report1Model
@{
    ViewBag.Title = "待收款统计";
    Layout = "~/Views/Shared/_Layout.cshtml"; 
}
<script type="text/javascript" src="@Url.Content("~/Content/js/jquery.treeview.async.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Content/js/jquery.treeview.edit.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Content/js/jquery.treeview.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Content/js/jquery.treeview.sortable.js")"></script>
<link href="@Url.Content("~/Content/js/jquery.treeview.css")" rel="stylesheet" type="text/css" />
@{
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    for (int i = 0; i < Model.DepartmentList.Count; i++)
    {
        var department = Model.DepartmentList[i];
        if (i == 0)
        {
            sb.Append(string.Format("{{ text:'{0}', uid:'{1}',utype:'Department',ulevel:'0' ", department.名称.Trim(), department.ID));
        }
        else
        {
            sb.Append(string.Format(",{{ text:'{0}', uid:'{1}',utype:'Department',ulevel:'0' ", department.名称.Trim(), department.ID));
        }

        if (department.分组List.Count > 0)
        {
            sb.Append(",children: [");

            for (int j = 0; j < department.分组List.Count; j++)
            {
                var group1 = department.分组List[j];
                if (j == 0)
                {
                    sb.Append(string.Format("{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'1' ", group1.名称.Trim(), group1.ID));
                }
                else
                {
                    sb.Append(string.Format(",{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'1' ", group1.名称.Trim(), group1.ID));
                }

                if (group1.分组List.Count > 0 || group1.ExtensionList.Count > 0)
                {
                    sb.Append(",children: [");

                    for (int k = 0; k < group1.分组List.Count; k++)
                    {
                        var group2 = group1.分组List[k];
                        if (k == 0)
                        {
                            sb.Append(string.Format("{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'2' ", group2.名称.Trim(), group2.ID));
                        }
                        else
                        {
                            sb.Append(string.Format(",{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'2' ", group2.名称.Trim(), group2.ID));
                        }

                        if (group2.分组List.Count > 0 || group2.ExtensionList.Count > 0)
                        {
                            sb.Append(",children: [");

                            for (int l = 0; l < group2.分组List.Count; l++)
                            {
                                var group3 = group2.分组List[l];
                                if (l == 0)
                                {
                                    sb.Append(string.Format("{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'3' ", group3.名称.Trim(), group3.ID));
                                }
                                else
                                {
                                    sb.Append(string.Format(",{{ text:'{0}', uid:'{1}',utype:'Group',ulevel:'3' ", group3.名称.Trim(), group3.ID));
                                }

                                if (group3.ExtensionList.Count > 0 || group3.ExtensionList.Count > 0)
                                {
                                    sb.Append(",children: [");

                                    for (int p = 0; p < group3.ExtensionList.Count; p++)
                                    {
                                        var ext = group3.ExtensionList[p];
                                        if (p == 0)
                                        {
                                            sb.Append(string.Format("{{ text:'{0}', uid:'{0}',utype:'Ext',ulevel:'3' ", ext.分机号码.Trim(), ext.用户名==null?"":ext.用户名.Trim()));
                                        }
                                        else
                                        {
                                            sb.Append(string.Format(",{{ text:'{0}', uid:'{0}',utype:'Ext',ulevel:'3' ", ext.分机号码.Trim(),ext.用户名==null?"":ext.用户名.Trim()));
                                        }

                                        sb.AppendLine("}");

                                    }

                                    sb.Append("]");
                                }
                                
                                sb.AppendLine("}");

                            }

                            for (int l = 0; l < group2.ExtensionList.Count; l++)
                            {
                                var ext = group2.ExtensionList[l];
                                if (l == 0 && group2.分组List.Count == 0)
                                {
                                    sb.Append(string.Format("{{ text:'{0}', uid:'{0}',utype:'Ext',ulevel:'3' ", ext.分机号码.Trim(), ext.用户名 == null ? "" : ext.用户名.Trim()));
                                }
                                else
                                {
                                    sb.Append(string.Format(",{{ text:'{0}', uid:'{0}',utype:'Ext',ulevel:'3' ", ext.分机号码.Trim(), ext.用户名 == null ? "" : ext.用户名.Trim()));
                                }

                                sb.AppendLine("}");

                            }
                            
                            sb.Append("]");
                        }

                        sb.AppendLine("}");

                    }

                    for (int k = 0; k < group1.ExtensionList.Count; k++)
                    {
                        var ext = group1.ExtensionList[k];
                        if (k == 0 && group1.分组List.Count == 0)
                        {
                            sb.Append(string.Format("{{ text:'{0}', uid:'{0}',utype:'Ext',ulevel:'3' ", ext.分机号码.Trim(), ext.用户名 == null ? "" : ext.用户名.Trim()));
                        }
                        else
                        {
                            sb.Append(string.Format(",{{ text:'{0}', uid:'{0}',utype:'Ext',ulevel:'3' ", ext.分机号码.Trim(), ext.用户名 == null ? "" : ext.用户名.Trim()));
                        }

                        sb.AppendLine("}");

                    }
                    
                    sb.Append("]");
                }

                sb.AppendLine("}");

            }

            sb.Append("]");
        }

        sb.AppendLine("}");

    }

    string data = sb.ToString();
}

@using (Html.BeginForm())
{ 
    @Html.HiddenFor(m => m.SelectItem, new { id = "SelectItem" });
    @Html.HiddenFor(m => m.IsExport, new { @class = "export" })
    <div class="report">
        <div class="system_contain_group col25">
            <h3 class="title">
                请选择部门、分组或分机</h3>
            <div class="system_scroll_container">
                <ul id="tree" />
            </div>
        </div>
        <div class="system_contain_group col75">
            <h3 class="title">
                其他条件</h3>
            <div class="system_input_contain">
                @Html.LabelFor(m => m.收费等级)
                @Html.DropDownListFor(m => m.收费等级, new SelectList(Model.收费等级List, "ID", "Name"), new { style = "input-text" })
                @Html.ValidationMessageFor(m => m.收费等级, new { style = "color:Red" })
            </div>
            <div class="system_input_contain">
                <input type="button" id="btnSearch" value="查询" />
                <input type="button" id="btnImport" value="导出Excel" />
            </div>
        </div>
    </div>
}
<script type="text/javascript">
    var tree;
    $(document).ready(function () {
        tree = $("#tree").ligerTree({ checkbox: false, parentIcon: null, childIcon: null, checkbox :true,
            data: [@Html.Raw(data)]});
        tree.collapseAll();
        $("#btnSearch").ligerButton(
        {
            click: function () {
                searchClick();
            }
        });
        $("#btnImport").ligerButton(
        {
            click: function () {
                exportClick();
            }
        });
     });

    function beforeSubmit() {
        var str = "";
        var list = tree.getChecked();
        
        for (var i = 0; i < list.length; i++) {
            if(list[i].data.utype == "Ext")
            {
            if (str == "")
            {
                str += list[i].data.uid;
                }
            else
            {
                str += "!" + list[i].data.uid;
                }
            }
        }
        
        $("#SelectItem").attr("value", str);
    }

    function exportClick() {
        $(".export").val("True");
        beforeSubmit();
        $("form").submit();
    }

    function searchClick() {
        $(".export").val("False");
        beforeSubmit();
        $("form").submit();
    }
</script>
